package databasetest;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Insert extends JInternalFrame {
	private JLabel lblNewLabel;
	private JLabel label;
	private JTextField code;
	private JTextField ea;
	private JButton btnNewButton;
	Connection conn;
	DatabaseTest d;
	

	
	/**
	 * Create the frame.
	 */
	public Insert(Connection c,DatabaseTest ds) {
		super("입력",true,true,true,false);
		setBounds(100, 100, 472, 217);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLabel());
		getContentPane().add(getCode());
		getContentPane().add(getEa());
		getContentPane().add(getBtnNewButton());
		this.conn = c;
		this.d = ds;

	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("코드");
			lblNewLabel.setBounds(23, 33, 62, 35);
		}
		return lblNewLabel;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("수량");
			label.setBounds(23, 95, 62, 35);
		}
		return label;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setBounds(97, 33, 200, 35);
			code.setColumns(10);
		}
		return code;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setColumns(10);
			ea.setBounds(97, 95, 200, 35);
		}
		return ea;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("입력");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try{
						String c = code.getText();
						int e = Integer.parseInt(ea.getText());
						
						String sql = "insert into ptbl(serial,code,ea) values(ptbl_serial.nextval,?,?)";
//						String sql = "insert into ptbl values("+c+","+e+")";
						PreparedStatement pstmt = conn.prepareStatement(sql);
						pstmt.setString(1, c);
						pstmt.setInt(2, e);
						
						pstmt.executeUpdate();
						if(d.select != null)d.select.setList();
					}catch(Exception ex){
						ex.printStackTrace();
					}
				}
			});
			btnNewButton.setBounds(309, 50, 97, 58);
		}
		return btnNewButton;
	}
	
	public void setInsert(String c,String e){
		code.setText(c);
		ea.setText(e);
		
	}
}
